/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License.You may obtain a copy of the License at
	http://www.mozilla.org/MPL/MPL-1.1.html
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import com.bourre.transitions.TweenMS;

import fever.display.effects.TweenEngine;

import fvaswing.effects.FvTweenEffect;

/**
 * Scale effect on <strong>AsWing components</strong>
 * 
 * <p>Effect changes the width / height of a target over a 
 * specified time interval.
 * You can specify the initial width / height with  the xFrom and yFrom values, 
 * the destination values with xTo and yTo, or the number of pixels to add 
 * with xBy and yBy.
 * 
 * <p>If you specify any two of the values (initial width, destination, or 
 * amount to add), {@link FvScale} calculates the third. <br />
 * If you specify all three, Fever ignores the xBy and yBy values.<br /> 
 * If you specify only the xTo and yTo values or the xBy and yBy values, 
 * {@link FvScale} sets xFrom and yFrom to be the object's current size.
 * 
 * <p>Example
 * {@code
 *   public function test() : Void
 *   {
 *     var button : JButton = new JButton( "move button" );
 *     
 *     var effect : FvScale = new FvScale( button );
 *     effect.addEventListener( FvEffectEvent.onEffectEndEVENT, this, _onEnd );
 *     effect.xTo = 300;
 *     effect.yTo = 200;
 *     effect.easing = Elastic.easeOut;
 *     
 *     effect.play();
 *   }
 * }
 * 
 * @author Romain Ecarnot
 */
class fvaswing.effects.FvScale extends FvTweenEffect
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private var _endX : Number;
	private var _endY : Number;
	private var _startX : Number;
	private var _startY : Number;
	
	private var _storeX : Number;
	private var _storeY : Number;
	
	
	//-------------------------------------------------------------------------
	// Public Properties
	//-------------------------------------------------------------------------
	
	/** Number of pixels to add to current component width. */
	public var xBy : Number;
	
	/** Initial component's width. */
	public var xFrom : Number;
	
	/** Destination width. */
	public var xTo : Number;
	
	/** Number of pixels to add to current component height. */
	public var yBy : Number;
	
	/** Initial component's height. */
	public var yFrom : Number;
	
	/** Destination height. */
	public var yTo : Number;
	
	
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 * 
	 * @param t AsVing component target
	 */
	public function FvScale( t )
	{
		super( t );
			
		xBy = Number.NaN;
		xFrom = Number.NaN;
		xTo = Number.NaN;
		yBy = Number.NaN;
		yFrom = Number.NaN;
		yTo = Number.NaN;
	}
	
	/**
	 * Starts effect.
	 * 
	 * <p>Stores current size for loop process.
	 */
	public function play() : Void
	{
		if( !_isPlaying )
		{
			_storeX = _instance.width;
			_storeY = _instance.height;
			
			super.play();
		}	
	}
	
	/**
	 * Ends effects.
	 * 
	 * <p>Overloads {@link FvTweenEffect#end} method.
	 */
	public function end() : Void
	{
		_tween.stop();
		
		_instance.width = _endX;		_instance.height = _endY;

		_endEffect();
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	/**
	 * Builds tweens instance
	 */
	private function _buildTween() : Void
	{
		_startX = ( isNaN( xFrom ) ) ?  ( _isRepeated ) ? _storeX : _instance.width : xFrom;
		_startY = ( isNaN( yFrom ) ) ?  ( _isRepeated ) ? _storeY : _instance.height : yFrom;
		
		_endX = ( isNaN( xBy ) ) ?  ( isNaN( xTo ) ) ?  _instance.width : xTo : xBy;
		_endY = ( isNaN( yBy ) ) ?  ( isNaN( yTo ) ) ?  _instance.height : yTo : yBy;
		_checkSize();
		
		_tween = TweenEngine.getInstance().getMultiTween( _instance, [ 'width', 'height' ], [ _endX, _endY ], duration, [ _startX, _startY ], easing );
		_tween.addEventListener( TweenMS.onMotionChangedEVENT, this );
		_tween.addEventListener( TweenMS.onMotionFinishedEVENT, this );
	}
	
	/**
	 * Checks ( and modify if necessary ) the destination size.<br />
	 * The destination size must superior to {@code Component.getMinimumSize()} 
	 * and inferior to {@code Component.getMaximumSize()}
	 */
	private function _checkSize() : Void
	{
		if( _endX < _instance.target.getMinimumWidth() )
		{
			 _endX = _instance.target.getMinimumWidth();
		}
		else if( _endX > _instance.target.getMaximumWidth() )
		{
			_endX = _instance.target.getMaximumWidth();	
		}
		
		if( _endY < _instance.target.getMinimumHeight() )
		{
			 _endY = _instance.target.getMinimumHeight();
		}
		else if( _endY > _instance.target.getMaximumHeight() )
		{
			_endY = _instance.target.getMaximumHeight();	
		}
	}
}